#include<func.h>
#include"train.h"
int main(int argc, char *argv[])
{
    //1. 创建套接字
    int clientfd =  socket(AF_INET, SOCK_STREAM, 0);
    ERROR_CHECK(clientfd, -1, "socket");

    struct sockaddr_in serveraddr;
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(atoi(argv[2]));
    serveraddr.sin_addr.s_addr = inet_addr(argv[1]);

    //2. 向服务器发起建立连接的请求
    int ret = connect(clientfd, 
                      (struct sockaddr *)&serveraddr, 
                      sizeof(serveraddr));
    ERROR_CHECK(ret, -1, "connect");
    printf("connect has completed.\n");

    fd_set rdset;

    while(1){
        FD_ZERO(&rdset);
        FD_SET(STDIN_FILENO,&rdset);
        FD_SET(clientfd,&rdset);
        char buff[1000]={0};
        select(10,&rdset,NULL,NULL,NULL);
        if(FD_ISSET(STDIN_FILENO,&rdset)){
            ret = read(STDIN_FILENO,buff,sizeof(buff));
            ERROR_CHECK(ret,-1,"send");

            char order[100]={0};//接收命令的数组
            char filename[100]={0};//接收文件名的数组
            splitChar(buff,order,filename);

            if(strcmp(order,"puts") == 0){
                trainSend(buff,clientfd);
                transferFile(clientfd,filename);
            }else{
                trainSend(buff,clientfd);
            }

            
            //memset(buff,0,sizeof(buff));
        }
        if(FD_ISSET(clientfd,&rdset)){
            //如果服务器端用的小火车发，就用这段代码。
            int length;
            ret = recv(clientfd, &length, 4, MSG_WAITALL);
           // printf(">>1. recv from server filelength:%d\n", length);
            
            char recvBuff[BUFFSIZE] = {0};//应用层接收缓冲区
            ret = recv(clientfd, recvBuff, length, MSG_WAITALL);
            char order[100]={0};//接收命令的数组
            char filename[100]={0};//接收文件名的数组
            printf("--recvBuff %s\n",recvBuff);
            //splitChar(recvBuff,order,filename);

            if(strncmp(recvBuff,"gets",4)==0){
                for(int i=0,j=5;j<strlen(recvBuff);i++,j++){
                    filename[i]=recvBuff[j];
                }
                recv_file(clientfd,filename);
            }else{
                printf(">>2. recv from server:%s\n", recvBuff);
                printf("---------------------\n");
            }

        }
    }
    /*
    //3. 下载文件
    //3.1 接收文件的名字
    int length;
    ret = recv(clientfd, &length, 4, MSG_WAITALL);
    printf(">>1. filename length:%d\n", length);

    //提出新的需求: 需要接收确定字节数的数据
    char recvBuff[1000] = {0};//应用层接收缓冲区
    ret = recv(clientfd, recvBuff, length, MSG_WAITALL); printf(">>2. recv filename:%s\n", recvBuff);

    //3.2 在客户端本地，要创建文件
    int fileFd = open(recvBuff, O_CREAT|O_RDWR, 0644);
    //3.3 接收文件的大小
    off_t fileSize;
    ret = recv(clientfd, &length, 4, MSG_WAITALL);
    printf("length: %d\n", length);
    ret = recv(clientfd, &fileSize, sizeof(fileSize), MSG_WAITALL);
    printf("> file length: %ld\n", fileSize);

    ftruncate(fileFd, fileSize);

    //3.4 接收文件的内容
    char * pbuf = (char*) mmap(NULL, fileSize, 
    PROT_READ|PROT_WRITE, 
    MAP_SHARED, fileFd, 0);
    ret = recv(clientfd, pbuf, fileSize, MSG_WAITALL);

    close(fileFd);
    munmap(pbuf, fileSize);*/
    close(clientfd);
}
